package io.agora.agorartcengine;

import android.content.Context;
import android.opengl.GLSurfaceView;

import java.util.Map;

import io.agora.rtc.RtcEngine;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class AgoraRenderViewFactory extends PlatformViewFactory {
    private final AgoraRtcEnginePlugin mEnginePlugin;

    AgoraRenderViewFactory(MessageCodec<Object> createArgsCodec, AgoraRtcEnginePlugin enginePlugin) {
        super(createArgsCodec);
        this.mEnginePlugin = enginePlugin;
    }

    @Override
    public PlatformView create(Context context, int id, Object o) {
        boolean local = (boolean) ((Map<String, Object>) o).get("local");
        if (local) {
            GLSurfaceView view = new GLSurfaceView(context);
            mEnginePlugin.addView(view, id);
            return new AgoraRendererView(context, view, mEnginePlugin, true);
        } else {
            GLSurfaceView view = (GLSurfaceView) RtcEngine.CreateRendererView(context);
            mEnginePlugin.addView(view, id);
            return new AgoraRendererView(context, view, mEnginePlugin, false);
        }
    }
}